// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import echarts from 'echarts'
import lodash from 'lodash'
import VueLodash from 'vue-lodash'
import {CONFIG} from './config/env'
import Cookies from 'js-cookie'
import axios from 'axios'


const moment = require('moment')
require('moment/locale/zh-cn');



Vue.prototype.$cookies = Cookies.get();



Vue.prototype.$echarts = echarts;

Vue.config.productionTip = false;

Vue.use(ElementUI);
Vue.use(VueLodash, lodash);
Vue.use(require('vue-moment'), {
  moment
});
console.log(Vue.moment().locale())
function initAJAX(cb) {
  if (Vue.prototype.$cookies[CONFIG.PROJECT.API.TOKENNAME]) {

    CONFIG.PROJECT.API.TOKEN = Vue.prototype.$cookies[CONFIG.PROJECT.API.TOKENNAME];
    cb&&cb();

  }else{

    if(CONFIG.PROJECT.API.DEBUG){

      cb&&cb();
    }else {
      window.location.href=CONFIG.PROJECT.API.FROM;
    }

  }

}

initAJAX(function () {

  axios.defaults.timeout = 3000;
  axios.defaults.headers['token'] = CONFIG.PROJECT.API.TOKEN;


  axios.interceptors.response.use((res) =>{
    // if(res.data) 配置token错误

    if(res.status!==200){
      app.$message({
        message: '接口请求不成功，请稍后重试',
        type: "warning"
      })

      return Promise.reject('接口请求不成功，请稍后重试');
    }
    ;
    if (res.data.result === "token-invalid") {

      app.$message({
        message: "权限过期, 请重新登录",
        type: "warning"
      })
      window.location.href = CONFIG.PROJECT.API.FROM;

    }
    if(res.data.result=='fail'){
      app.$message({
        message: res.data.message,
        type: "warning"
      })

      return Promise.reject(res.data.message);
    }

    return Promise.resolve(res);

  }, (error) => {
    app.$message({
      message: '网络错误，请稍后重试',
      type: "warning"
    })

    return Promise.reject('网络错误，请稍后重试');
  });


});


/* eslint-disable no-new */
let app=new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
